package sharetime.web.view.freemarker;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.view.freemarker.FreeMarkerView;

import sharetime.util.RequestUtils;
import sharetime.util.SecurityHelper;

/**
 * add base and securityHelper to model, so it can be accessed. e.g.
 * ${securityHelper.principal?default("")}
 * 
 * @author rockmaple
 * 
 */
public class CustomFreeMarkerView extends FreeMarkerView {

	private static final Logger logger = LoggerFactory
			.getLogger(CustomFreeMarkerView.class);

	@SuppressWarnings("unchecked")
	@Override
	protected void exposeHelpers(Map model, HttpServletRequest request)
			throws Exception {
		model.put("base", RequestUtils.getResourceBase(request));
		model.put("securityHelper", new SecurityHelper());
		model.put("req", request);
		
		// 放在服务器端计算还是客户端计算?放在客户端计算会节省服务器端的资源
		/*
		String userAgent = request.getHeader("User-Agent").toLowerCase();
		if (userAgent.contains("mozilla")) {
			userAgent = "mozilla";
		} else if (userAgent.contains("msie") && userAgent.contains("6.0")) {
			userAgent = "ie ie6";
		} else if (userAgent.contains("msie") && userAgent.contains("7.0")) {
			userAgent = "ie ie7";
		} else if (userAgent.contains("opera")) {
			userAgent = "opera";
		} else if (userAgent.contains("webkit")) {
			userAgent = "safari";
		} else {
			userAgent = "";
		}
		logger.debug("user agent is: " + userAgent);
		model.put("userAgent", userAgent);
		*/
		
	}
}
